﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
namespace PCCS.Management
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ClearHeaders();
            Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
            Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
            Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
            Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
            Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
            Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
            Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
            Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
            Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
            Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1 

            Response.Clear();
            Response.ContentType = "text/plain";
            try
            {
                String fileName = Request.Files[0].FileName;
                Stream fileStream = Request.Files[0].InputStream;
                String extension = fileName.Substring(fileName.LastIndexOf('.'));//扩展名
                String imageURL = this.UploadImage(fileName, fileStream);

                if (String.IsNullOrEmpty(imageURL))
                {
                    throw new Exception("Invoke image upload services exception.");
                }


                Response.Write("{\"Status\": \"Success\", \"FileName\": \"" + fileName + "\",\"Extension\": \"" + extension + "\", \"ImageURL\": \"" + imageURL + "\"}");
            }
            catch (Exception ex)
            {
                Response.Write("{\"Status\": \"Error\", \"Exception\": \"" + ex.Message + "\"}");
            }
            Response.End();
        }
        /// <summary>
        /// UploadHomePageImage
        /// </summary>
        /// <param name="originalFileName">Client file name</param>
        /// <param name="image">image stream</param>
        /// <returns></returns>
        public String UploadImage(String originalFileName, Stream image)
        {

            String m_Result = Guid.NewGuid().ToString("D") + Path.GetExtension(originalFileName);
            //FileServiceSoapClient m_FileServiceSoapClient = new FileServiceSoapClient();
            //MemoryStream m = new MemoryStream();
            FileStream fs = File.Open(Server.MapPath("~/UpLoad/Portfolio/") + m_Result, FileMode.OpenOrCreate);
            try
            {
                int size = 2048;
                byte[] data = new byte[2048];

                while (true)
                {
                    // 写入数据
                    size = image.Read(data, 0, data.Length);
                    if (size > 0)
                    {
                        fs.Write(data, 0, size);
                    }
                    else
                    {
                        break;
                    }
                }

                return m_Result;

            }
            catch (Exception)
            {
                image.Close();
                image.Dispose();

                return String.Empty;
            }
            finally
            {
                fs.Close();
                fs.Dispose();
            }

        }

    }
}
